import scrapy
from scrapy import Request
import pandas as pd

from Project.DogRecruitment.DogRecruitment.items import FindJobItem, FindJobDetailItem, CompanyDetailItem


class Company_Detail(scrapy.Spider):
    name = "CompanyDetail"
    data = pd.read_csv(r"D:\code\Python-Project\Scrapy\Project\DogRecruitment\FindJob.csv",encoding='utf-8')
    company_id = set(data['enterpriseId'].values)
    start_urls = [f"https://www.5iai.com/api/enterprise/detail/{i}" for i in company_id]
    print(start_urls)
    def start_requests(self):
        for url in self.start_urls:
            yield Request(url,dont_filter=True,callback=self.parse)

    def parse(self, response, **kwargs):
        r = response.json()['data']
        item = CompanyDetailItem()
        item["id"] = r["id"]
        item["enterpriseId"] = r["enterpriseId"]
        item["logo"] = r["logo"]
        item["shortName"] = r["shortName"]
        item["industry"] = r["industry"]
        item["econKind"] = r["econKind"]
        item["startDate"] = r["startDate"]
        item["registCapi"] = r["registCapi"]
        item["personScope"] = r["personScope"]
        item["website"] = r["website"]
        item["email"] = r["email"]
        item["phone"] = r["phone"]
        item["slogan"] = r["slogan"]
        item["introduction"] = r["introduction"]
        item["photo"] = r["photo"]
        item["label"] = r["label"]
        item["postCode"] = r["postCode"]
        item["recruitJobNum"] = r["recruitJobNum"]
        item["totalPublicJobNum"] = r["totalPublicJobNum"]
        if r['enterpriseAddress']:
            item["provinceCode"] = r['enterpriseAddress']["provinceCode"]
            item["cityCode"] = r['enterpriseAddress']["cityCode"]
            item["regionCode"] = r['enterpriseAddress']["regionCode"]
            item["detailedAddress"] = r['enterpriseAddress']["detailedAddress"]
            item["remarks"] = r['enterpriseAddress']["remarks"]
        else:
            item["provinceCode"] = ""
            item["cityCode"] = ""
            item["regionCode"] = ""
            item["detailedAddress"] = ""
            item["remarks"] = ""
        yield item
